#!/bin/bash

# blog-web Jenkins 完整构建和部署脚本
# 可以直接复制到 Jenkins 的 Execute shell 中使用

set -e

# 使用 Jenkins 的 WORKSPACE 环境变量
WORKSPACE_DIR="${WORKSPACE:-/var/jenkins_home/workspace/blog-web}"
PROJECT_DIR="${WORKSPACE_DIR}/blog-web"

# 如果 blog-web 子目录不存在，使用工作空间根目录
if [ ! -d "$PROJECT_DIR" ]; then
    PROJECT_DIR="$WORKSPACE_DIR"
fi

echo "========================================"
echo "blog-web 构建和部署"
echo "工作空间: $WORKSPACE_DIR"
echo "项目目录: $PROJECT_DIR"
echo "========================================"

# 检查项目目录是否存在
if [ ! -d "$PROJECT_DIR" ]; then
    echo "错误: 项目目录不存在: $PROJECT_DIR"
    exit 1
fi

# 进入项目目录
cd "$PROJECT_DIR" || exit 1
echo "当前目录: $(pwd)"
echo ""

# 检测 Docker 命令
DOCKER_CMD="docker"
if ! docker ps >/dev/null 2>&1; then
    if sudo docker ps >/dev/null 2>&1; then
        DOCKER_CMD="sudo docker"
        echo "使用 sudo 执行 Docker 命令"
    else
        echo "错误: 无法访问 Docker"
        exit 1
    fi
fi

# 检查并拉取 Node.js 镜像
echo "检查 Node.js 镜像..."
if ! $DOCKER_CMD images --format "{{.Repository}}:{{.Tag}}" | grep -q "node:18-alpine"; then
    echo "拉取 node:18-alpine 镜像..."
    $DOCKER_CMD pull node:18-alpine
fi

# 步骤1: 安装依赖（限制资源）
echo ""
echo "步骤1: 安装依赖（限制内存 1.5GB，CPU 1.5核）..."
$DOCKER_CMD run --rm \
    --memory="1.5g" \
    --memory-swap="1.5g" \
    --cpus="1.5" \
    -v "$PROJECT_DIR:/app" \
    -w /app \
    node:18-alpine \
    sh -c "npm config set registry https://registry.npmmirror.com && npm install --prefer-offline --no-audit --no-fund"

# 步骤2: 构建项目（限制资源）
echo ""
echo "步骤2: 构建项目（限制内存 1.5GB，CPU 1.5核）..."
$DOCKER_CMD run --rm \
    --memory="1.5g" \
    --memory-swap="1.5g" \
    --cpus="1.5" \
    -v "$PROJECT_DIR:/app" \
    -w /app \
    -e NODE_OPTIONS="--max-old-space-size=1024" \
    node:18-alpine \
    npm run build

# 检查构建结果
BUILD_DIR="${PROJECT_DIR}/dist"
if [ ! -d "$BUILD_DIR" ] || [ -z "$(ls -A $BUILD_DIR)" ]; then
    echo "错误: 构建失败或构建目录为空"
    exit 1
fi

echo "构建成功: $BUILD_DIR"
echo ""

# 步骤3: 部署项目
echo "步骤3: 部署项目..."

CONTAINER_NAME="blog-web"
PORT=3001
NGINX_CONF="${PROJECT_DIR}/nginx.conf"

# 清理旧容器
echo "清理旧容器..."
$DOCKER_CMD stop $CONTAINER_NAME 2>/dev/null || true
$DOCKER_CMD rm $CONTAINER_NAME 2>/dev/null || true

# 检查并拉取 Nginx 镜像
echo "检查 Nginx 镜像..."
if ! $DOCKER_CMD images --format "{{.Repository}}:{{.Tag}}" | grep -q "nginx:alpine"; then
    echo "拉取 nginx:alpine 镜像..."
    $DOCKER_CMD pull nginx:alpine
fi

# 启动新容器
echo "启动新容器..."
if [ -f "$NGINX_CONF" ]; then
    $DOCKER_CMD run -d \
        --name $CONTAINER_NAME \
        --restart=unless-stopped \
        -p $PORT:80 \
        -v $BUILD_DIR:/usr/share/nginx/html:ro \
        -v $NGINX_CONF:/etc/nginx/conf.d/default.conf:ro \
        nginx:alpine
else
    echo "警告: Nginx 配置文件不存在，使用默认配置"
    $DOCKER_CMD run -d \
        --name $CONTAINER_NAME \
        --restart=unless-stopped \
        -p $PORT:80 \
        -v $BUILD_DIR:/usr/share/nginx/html:ro \
        nginx:alpine
fi

# 等待容器启动
sleep 3

# 检查容器状态
if $DOCKER_CMD ps --format "{{.Names}}" | grep -q "^${CONTAINER_NAME}$"; then
    echo ""
    echo "========================================"
    echo "部署成功！"
    echo "容器名称: $CONTAINER_NAME"
    echo "服务端口: $PORT"
    echo "访问地址: http://服务器IP:$PORT"
    echo "========================================"
    echo ""
    echo "容器信息:"
    $DOCKER_CMD ps --filter "name=^${CONTAINER_NAME}$"
else
    echo ""
    echo "========================================"
    echo "部署失败！容器未启动"
    echo "========================================"
    echo "容器日志:"
    $DOCKER_CMD logs $CONTAINER_NAME
    exit 1
fi

